﻿using Microsoft.Extensions.Configuration;
using SharpZip.Net;

namespace SharpZip.Cmd
{
    internal class Program
    {
        static void Main(string[] args)
        {
            IConfiguration config = new ConfigurationBuilder().AddCommandLine(args).Build();

            var key = config["key"];
            var iv = config["iv"];
            var ignore = config["ignore"];
            var dir = config["dir"];
            var file = config["file"];
            var zip = config["zip"];
            var output = config["output"];

            if (string.IsNullOrWhiteSpace(output))
            {
                Console.WriteLine("请设置 output.");
                return;
            }
            output = output.Trim();

            if (string.IsNullOrWhiteSpace(key))
            {
                Console.WriteLine("请设置 key.");
                return;
            }
            key = key.Trim();

            if (string.IsNullOrWhiteSpace(iv))
            {
                Console.WriteLine("请设置 iv.");
                return;
            }
            iv = iv.Trim();

            if (string.IsNullOrWhiteSpace(file))
            {
                Console.WriteLine("请设置 file.");
                return;
            }
            file = file.Trim();

            if (string.IsNullOrWhiteSpace(dir))
            {
                Console.WriteLine("请设置 dir.");
                return;
            }
            dir = dir.Trim();

            var ignorePaths = new List<string>();

            if (!string.IsNullOrWhiteSpace(ignore))
            {
                var paths = ignore.Split(",");
                foreach (var path in paths)
                {
                    var tmp = path.Trim();
                    if (!string.IsNullOrWhiteSpace(tmp))
                    {
                        Console.WriteLine(tmp);
                        ignorePaths.Add(tmp);
                    }
                }
            }

            var aesKey = new AesEncryptorKey(key, iv);

            if (string.Equals(output, "zip", StringComparison.OrdinalIgnoreCase))
            {
                Console.WriteLine("开始压缩...");
                try
                {
                    AesTextZipFile.CreateFromDirectory(dir, file, aesKey, ignorePaths.ToArray());
                    Console.WriteLine("压缩完成.");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("压缩出现错误.");
                    Console.WriteLine(ex.ToString());
                }

            }
            else
            if (string.Equals(output, "unzip", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    Console.WriteLine("开始解压...");
                    AesTextZipFile.ExtractToDirectory(file, dir, aesKey);
                    Console.WriteLine("解压完成.");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("解压出现错误.");
                    Console.WriteLine(ex.ToString());
                }
            }
            else
            {
                Console.WriteLine($"output 参数 {output} 无效,有效值为 zip 或 unzip.");
            }
        }
    }
}
